import 'yl_num.dart';

extension YLString on String {
  int toInt() => int.tryParse(this) ?? 0;
  double toDouble() => double.tryParse(this) ?? 0.0;

  List<String> toList({
    int? lengthSplit,
    Pattern? patternSplit,
  }) {
    late List<String> resultList = <String>[];
    if (lengthSplit != null) {
      final int length = this.length;
      if (lengthSplit == 0 || length < lengthSplit) {
        resultList = <String>[this];
      } else {
        final int remainder = length % lengthSplit;
        final int count = length ~/ lengthSplit;
        resultList = List<String>.generate(count, (int i) {
          final int start = i * lengthSplit;
          return substring(start, start + lengthSplit);
        }).toList();
        if (remainder != 0) {
          resultList.add(substring(length - remainder));
        }
      }
    } else {
      resultList = split(patternSplit ?? '');
    }
    return resultList;
  }

//是否是合法身份证
  bool isIDCard() {
    late bool result = false;
    if (isNotEmpty) {
      final RegExp regExp = RegExp(r'\d{17}[0-9Xx]$');
      if (regExp.hasMatch(this)) {
        final String forward17 = substring(0, 17);
        final String behind1 = substring(17).toUpperCase();
        final List<String> splitList = forward17.toList();
        // Coefficient for id.
        final List<String> modulusList =
            '7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2'.toList(patternSplit: ',');
        late int sum = 0;
        for (int i = 0; i < 17; i++) {
          final int item1 = int.parse(splitList[i]);
          final int item2 = int.parse(modulusList[i]);
          sum += item1 * item2;
        }
        final int remainder = sum % 11;
        late final Map<String, String> mapping = <String, String>{
          '0': '1',
          '1': '0',
          '2': 'X',
          '3': '9',
          '4': '8',
          '5': '7',
          '6': '6',
          '7': '5',
          '8': '4',
          '9': '3',
          '10': '2'
        };
        result = mapping[remainder.toString()] == behind1;
      }
    }
    return result;
  }

// 是否是手机号
  bool isPhone() => RegExp(
          r'^((13[^4]|14[5|7|9]|15[^4]|166|17[^0149]|18\d|19[^4])\d{8}|134[^9]\d{7})$')
      .hasMatch(this);
  // 是否是密码
  bool isPwd() =>
      RegExp(r'^(?=.*[0-9])(?=.*[a-zA-Z])(.{7,20})$').hasMatch(this);

  bool isRegular(String regular) => RegExp(regular).hasMatch(this);

  String toReplace(String replace, {String? regular}) {
    return (this).replaceAll(RegExp(regular ?? r'XXX'), replace);
  }

// 保留几位小数 省略末尾0
  String keepShortDigits(int length, {bool round = true}) {
    String allStr = (this).keepFractionDigits(length, round: round);
    List<String> nomreList = allStr.split('.');
    List numList = [];
    List lastList = nomreList.last.toList().reversed.toList();
    for (var element in lastList) {
      if (element == '0' && numList.isEmpty) {
        continue;
      }
      numList.insert(0, element);
    }
    return numList.isEmpty
        ? nomreList.first
        : "${nomreList.first}.${numList.join()}";
  }

//保留几位小数
  String keepFractionDigits(int length, {bool round = true}) {
    return round
        ? (this).toDouble().toStringAsFixed(length)
        : (this).toDouble().toYLPrecision(length).toString();
  }
}
